package cn.tedu.text;

import java.sql.*;

public class Test {
    public static void main(String[] args) {
        //add();
        //delete();
        //update();
        select();


    }

    public static void add() {
        //添加数据
        Connection conn=null;
        PreparedStatement pt=null;
        int re;


        try {
            conn=Dao.conn();

            String sql="insert  into students values (101,'张三','男','1978-10-02','95033' )";
            pt=conn.prepareStatement(sql);

            re=pt.executeUpdate();
            System.out.println(re);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        Dao.close(null,pt,conn);


    }

    public static void delete() {
    //删除数据
        Connection conn=null;
        PreparedStatement pt=null;
            int re;


        try {
            conn=Dao.conn();

            String sql="delete from students where sno=101 ";
            pt=conn.prepareStatement(sql);

            re=pt.executeUpdate();
            System.out.println(re);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
     Dao.close(null,pt,conn);
    }

    public static void update() {
    //修改数据
        Connection conn=null;
        PreparedStatement pt=null;
        int re;


        try {
            conn=Dao.conn();

            String sql="update students set sname='李四',ssex='女'where sno=101";
            pt=conn.prepareStatement(sql);

            re=pt.executeUpdate();
            System.out.println(re);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        Dao.close(null,pt,conn);
    }

    public static void select() {
        //查询数据
        Connection conn=null;
        PreparedStatement pt=null;
        ResultSet re=null;

        try {
            conn=Dao.conn();//连接数据库
            String sql="select * from students";//写sql语句，对数据库进行操作
            pt=conn.prepareStatement(sql);//创建传输器
            re=pt.executeQuery();
            int row=re.getMetaData().getColumnCount();//得到表数据的总数
            while (re.next()){
                for (int i = 1; i <=row ; i++) {
                    System.out.print(re.getString(i)+" ");
                }
                System.out.println();
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        Dao.close(re,pt,conn);

    }
}
